package dao;

import entity.Attraction;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DBUtil;

import java.sql.SQLException;
import java.util.List;

public class AttractionDao {
    QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    //可以将下划线命名的改为驼峰命名
    BeanListHandler<Attraction> blh = new BeanListHandler<>(
            Attraction.class, new BasicRowProcessor(new GenerousBeanProcessor()));

    /**
     * 添加景点
     */
    public int add(Attraction attraction){
        String sql = "insert into attraction values (?,?,?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql, attraction.getId(), attraction.getName(),
                    attraction.getRating(), attraction.getPrice(), attraction.getAddress(),
                    attraction.getOpeningHours(), attraction.getIntroduce(),
                    attraction.getPictureUrl(), attraction.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 单表查询
     */
    public List<Attraction> query(String sql, Object... params){
        try {
            return queryRunner.query(sql, blh, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
